package com.itheima.advantagesup.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.advantagesup.dto.UserInfoDTO;
import com.itheima.advantagesup.vo.UsersPageBeanVO;
import com.itheima.advantagesup.entity.SysUser;
import com.itheima.advantagesup.mapper.SysUserMapper;
import com.itheima.advantagesup.service.SysUserService;
import com.itheima.advantagesup.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * SysUserServiceImpl
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/7/26 14:58
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public UsersPageBeanVO page(String login_name, Integer status, String phone, Integer page, Integer limit) {
        //设置分页参数
        PageHelper.startPage(page,limit);
        //模糊查询得到SysUser集合
        List<SysUser> sysUsers = sysUserMapper.page(login_name,status,phone);
        Page<SysUser> r1 = (Page<SysUser>) sysUsers;
        List<UserInfoVO> l = new Page<>();
        for (SysUser sysUser : sysUsers) {
            UserInfoVO userInfoVO = new UserInfoVO();
            //把SysUser转化为UserInfoVO
            UserInfoVO u = sysUserToUserInfoVO(sysUser, userInfoVO);
            l.add(u);
        }
        Page<UserInfoVO> r = (Page<UserInfoVO>) l;
        //封装
        UsersPageBeanVO p = new UsersPageBeanVO(r1.getTotal(),r.getPages(),limit,1,r.getResult());
        return p;
    }

    @Override
    public void add(UserInfoDTO userInfoDTO) {
       SysUser sysUser =  userInfoDTOToSysUser(userInfoDTO);
       sysUser.setCreateDate(LocalDateTime.now());
       sysUser.setUpdateDate(LocalDateTime.now());
       //生成随机id
       sysUser.setId(UUID.randomUUID().toString());
       sysUserMapper.add(sysUser);
    }

    @Override
    public void delete(String id) {
        sysUserMapper.delete(id);
    }

    @Override
    public UserInfoVO getUserById(String id) {
        SysUser sysUser = sysUserMapper.getUserById(id);
        //把SysUser转化为UserInfoVO
        UserInfoVO userInfoVO = sysUserToUserInfoVO(sysUser,new UserInfoVO());
        return userInfoVO;
    }

    @Override
    public void update(UserInfoDTO userInfoDTO) {
        //把UserInfoDTO转化为SysUser
        SysUser sysUser = userInfoDTOToSysUser(userInfoDTO);
        sysUser.setUpdateDate(LocalDateTime.now());
        sysUserMapper.update(sysUser);
    }


    /**
     * 把UserInfoDTO转换为SysUser
     * @return {@link SysUser}
     */
    private SysUser userInfoDTOToSysUser(UserInfoDTO userInfoDTO) {
        SysUser sysUser = new SysUser();
        sysUser.setId(userInfoDTO.getId());
        sysUser.setLoginName(userInfoDTO.getLogin_name());
        sysUser.setPassword(userInfoDTO.getPassword());
        sysUser.setName(userInfoDTO.getName());
        sysUser.setEmail(userInfoDTO.getEmail());
        sysUser.setPhone(userInfoDTO.getPhone());
        sysUser.setLoginIp(userInfoDTO.getLogin_ip());
        sysUser.setLoginDate(userInfoDTO.getLogin_date());
        sysUser.setCreateDate(userInfoDTO.getCreate_date());
        sysUser.setUpdateDate(userInfoDTO.getUpdate_date());
        sysUser.setDelFlag(userInfoDTO.getDel_flag());
        sysUser.setRoleId(JSON.toJSONString(userInfoDTO.getRole_id()));
        sysUser.setStatus(userInfoDTO.getStatus());
        return sysUser;
    }


    /**
     * 把SysUser转换为UserInfoVO
     * @param sysUser
     * @param userInfoVO
     * @return {@link UserInfoVO}
     */
    private UserInfoVO sysUserToUserInfoVO(SysUser sysUser,UserInfoVO userInfoVO){
        //反序列化把role_id变为json(因为web接收数据需要json)
        List<String> list1 = JSON.parseArray(sysUser.getRoleId(), String.class);
        List<String> list2 = new ArrayList<>();
        //遍历role_id集合得到role_name集合
        for (String s : list1) {
            String roleName = sysUserMapper.getRoleName(s);

        }
        userInfoVO.setId(sysUser.getId());
        userInfoVO.setLogin_name(sysUser.getLoginName());
        userInfoVO.setPassword(sysUser.getPassword());
        userInfoVO.setName(sysUser.getName());
        userInfoVO.setEmail(sysUser.getEmail());
        userInfoVO.setPhone(sysUser.getPhone());
        userInfoVO.setLogin_ip(sysUser.getLoginIp());
        userInfoVO.setLogin_date(sysUser.getLoginDate());
        userInfoVO.setCreate_date(sysUser.getCreateDate());
        userInfoVO.setUpdate_date(sysUser.getUpdateDate());
        userInfoVO.setDel_flag(sysUser.getDelFlag());
        userInfoVO.setRole_id(list1);
        userInfoVO.setStatus(sysUser.getStatus());
        userInfoVO.setRolename(list2);
        return userInfoVO;
    }
}
